home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / DirectInput / DIConfig / flextree.h < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-08  |  8.3 KB  |  298 lines

  1. //-----------------------------------------------------------------------------
  2. // File: flextree.h
  3. //
  4. // Desc: Implements a tree class, similar to a Windows tree control,
  5. //       based on CFlexWnd.  It is used by the page to display the action
  6. //       list when the user wishes to assign an action to a control.
  7. //
  8. // Copyright (C) 1999-2001 Microsoft Corporation. All Rights Reserved.
  9. //-----------------------------------------------------------------------------
  10.  
  11. #ifndef __FLEXTREE_H__
  12. #define __FLEXTREE_H__
  13.  
  14.  
  15. #include "flexscrollbar.h"
  16.  
  17.  
  18. class CFTItem;
  19. class CFlexTree;
  20.  
  21.  
  22. typedef struct {
  23.     CFlexTree *pTree;
  24.     CFTItem *pItem, *pOldItem;
  25.     POINT point;
  26.     HDC hDC;
  27.     WPARAM fwKeys;
  28.     BOOL bLeft;
  29. } FLEXTREENOTIFY;
  30.  
  31. enum {
  32.     FTN_CLICK,
  33.     FTN_OWNERDRAW,
  34.     FTN_SELCHANGED,
  35.     FTN_MOUSEOVER
  36. };
  37.  
  38. enum {
  39.     CLMF_NONE =            0x00000000,
  40.     CLMF_TEXTCOLOR =    0x00000001,
  41.     CLMF_BKCOLOR =        0x00000002,
  42.     CLMF_BKMODE =        0x00000004,
  43.     CLMF_BKEXTENDS =    0x00000008,
  44.     CLMF_FONT =            0x00000010,
  45.     CLMF_LINECOLOR = 0x00000020,
  46.     CLMF_ALL =            0x0000003f
  47. };
  48.  
  49. struct _CAPTIONLOOK;
  50. typedef struct _CAPTIONLOOK {
  51.     _CAPTIONLOOK() : dwSize(sizeof(_CAPTIONLOOK)), dwMask(CLMF_NONE),
  52.         rgbTextColor(RGB(0,0,0)), rgbBkColor(RGB(255,255,255)), rgbLineColor(RGB(255,0,0)), nBkMode(TRANSPARENT),
  53.         bBkExtends(FALSE), hFont(NULL) {}
  54.     DWORD dwSize;
  55.     DWORD dwMask;
  56.     COLORREF rgbTextColor, rgbBkColor, rgbLineColor, nBkMode;
  57.     BOOL bBkExtends;
  58.     HFONT hFont;
  59. } CAPTIONLOOK;
  60.  
  61. typedef enum {
  62.     ATTACH_FIRSTCHILD,
  63.     ATTACH_LASTCHILD,
  64.     ATTACH_FIRSTSIBLING,
  65.     ATTACH_LASTSIBLING,
  66.     ATTACH_BEFORE,
  67.     ATTACH_AFTER
  68. } ATTACHREL;
  69.  
  70.  
  71. class CFlexTree : public CFlexWnd
  72. {
  73. friend class CFTItem;
  74. public:
  75.     CFlexTree();
  76.     ~CFlexTree();
  77.  
  78.     // creation
  79.     BOOL Create(HWND hParent, const RECT &, BOOL bVisible = TRUE, BOOL bOwnerDraw = FALSE);
  80.  
  81.     // look
  82.     void SetScrollBarColors(COLORREF bk, COLORREF fill, COLORREF line);
  83.     void SetDefCaptionLook(const CAPTIONLOOK &, BOOL bSel = FALSE);
  84.     void GetDefCaptionLook(CAPTIONLOOK &, BOOL bSel = FALSE) const;
  85.     void SetDefMargin(const RECT &);
  86.     void GetDefMargin(RECT &) const;
  87.     void SetRootChildIndent(int);
  88.     int GetRootChildIndent() const;
  89.     void SetDefChildIndent(int);
  90.     int GetDefChildIndent() const;
  91.     void SetBkColor(COLORREF);
  92.     COLORREF GetBkColor() const;
  93.  
  94.     // adding default type items
  95.     CFTItem *DefAddItem(LPCTSTR tszCaption, CFTItem *to, ATTACHREL rel = ATTACH_AFTER);
  96.     CFTItem *DefAddItem(LPCTSTR tszCaption, ATTACHREL rel = ATTACH_AFTER);
  97.  
  98.     // freeing
  99.     void FreeAll();
  100.  
  101.     // root access
  102.     operator CFTItem *() const {return m_pRoot;}
  103.     CFTItem *GetRoot() const {return m_pRoot;}
  104.  
  105.     // access
  106.     CFTItem *GetFirstItem() const;
  107.     CFTItem *GetLastItem() const;
  108.     CFTItem *GetFirstVisibleItem() const;
  109.     CFTItem *GetItemFromPoint(POINT point) const;
  110.  
  111.     // selection
  112.     void SetCurSel(CFTItem *);
  113.     CFTItem *GetCurSel() const;
  114.  
  115.     // finding
  116.     CFTItem *FindItem(const GUID &guid, void *pUserData) const;
  117.     CFTItem *FindItemEx(const GUID &guid, DWORD dwFindType, void *pVoid) const;
  118.  
  119. protected:
  120.     virtual BOOL OnEraseBkgnd(HDC hDC) {return TRUE;}
  121.     virtual void OnPaint(HDC hDC);
  122.     virtual void OnMouseOver(POINT point, WPARAM fwKeys);
  123.     virtual void OnClick(POINT point, WPARAM fwKeys, BOOL bLeft);
  124.     virtual void OnWheel(POINT point, WPARAM wParam);
  125.     virtual LRESULT WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
  126.  
  127.     // event notification firing
  128.     void FireClick(CFTItem *pItem, POINT point, WPARAM fwKeys, BOOL bLeft);
  129.     BOOL FireOwnerDraw(CFTItem *pItem, HDC hDC);
  130.     void FireSelChanged(CFTItem *pItem, CFTItem *pOld);
  131.  
  132. private:
  133.     CFTItem *m_pRoot;    // root item
  134.     CFTItem *m_pCurSel;    // selected item
  135.     CFTItem *m_pLastAdded;
  136.     BOOL m_bOwnerDraw;
  137.     POINT m_ptScrollOrigin;
  138.     COLORREF m_rgbBkColor;
  139.     CAPTIONLOOK m_clDefNormal, m_clDefSelected;
  140.     RECT m_defmargin;
  141.     int m_nDefChildIndent;
  142.  
  143.     // scrolling
  144.     int m_nVertSBWidth;
  145.     int m_nHorzSBHeight;
  146.     BOOL m_bVertSB, m_bHorzSB;
  147.     CFlexScrollBar m_VertSB, m_HorzSB;
  148.     int m_nTotalWidth;
  149.  
  150.     // helpers
  151.     BOOL m_bNeedPaintBkgnd;
  152.     void SetDirty();
  153.     void InternalPaint(HDC hDC);
  154.     BOOL m_bDirty;
  155.     void Calc();
  156.     void CalcItems();
  157.     BOOL IsMine(CFTItem *pItem);
  158.     void LosePointer(CFTItem *pItem);
  159. };
  160.  
  161. class CFTItem
  162. {
  163. friend class CFlexTree;
  164. public:
  165.     CFTItem();
  166.     ~CFTItem();
  167.  
  168.     // operations
  169.     BOOL IsOut() const;
  170.     BOOL IsExpanded() const {return m_bExpanded;}
  171.     void Expand(BOOL bAll = FALSE);
  172.     void ExpandAll() {Expand(TRUE);}
  173.     void Collapse(BOOL bAll = FALSE);
  174.     void CollapseAll() {Collapse(TRUE);}
  175.     void EnsureVisible();
  176.     void Invalidate();
  177.     
  178.     // caption
  179.     void SetCaptionLook(const CAPTIONLOOK &, BOOL bSel = FALSE);
  180.     void GetCaptionLook(CAPTIONLOOK &, BOOL bSel = FALSE) const;
  181.     void SetCaption(LPCTSTR);
  182.     LPCTSTR GetCaption() const;
  183.     BOOL HasCaption() const {return GetCaption() != NULL;}
  184.     void SetMargin(const RECT &);
  185.     void GetMargin(RECT &) const;
  186.  
  187.     // attach/detachment
  188.     void Detach();            // detaches this leaf/branch from parent.  (does not affect children, who may still be attached to this)
  189.     void FreeChildren();    // detach and free each child (which in turn frees all their's, etc.)
  190.     BOOL Attach(CFTItem *to, ATTACHREL rel);
  191.     BOOL Attach(CFTItem &to, ATTACHREL rel) {return Attach(&to, rel);}
  192.     BOOL IsOnTree() const;
  193.     BOOL IsAttached() const;
  194.     BOOL IsAlone() const;
  195.  
  196.     // family access
  197.     CFlexTree *GetTree() const {return m_pTree;}
  198.     CFTItem *GetParent() const {return m_pParent;}
  199.     CFTItem *GetPrevSibling() const {return m_pPrev;}
  200.     CFTItem *GetNextSibling() const {return m_pNext;}
  201.     CFTItem *GetFirstChild() const {return m_pFirst;}
  202.     CFTItem *GetLastChild() const {return m_pLast;}
  203.     CFTItem *GetNextOut() const;
  204.     CFTItem *GetNext(BOOL bOutOnly = FALSE) const;
  205.     BOOL HasChildren() const {return m_pFirst != NULL;}
  206.  
  207.     // dimension access
  208.     void GetItemRect(RECT &) const;
  209.     void GetBranchRect(RECT &) const;
  210.  
  211.     // user guid/data operations
  212.     BOOL IsUserGUID(const GUID &check) const {return IsEqualGUID(m_UserGUID, check);}
  213.     void SetUserGUID(const GUID &set)  {m_UserGUID = set;}
  214.     const GUID &GetUserGUID() const {return m_UserGUID;}
  215.     void SetUserData(void *p) {m_pUserData = p;}
  216.     void *GetUserData() const {return m_pUserData;}
  217.  
  218.     // selection
  219.     BOOL IsSelected() const;
  220.  
  221.     // owner draw
  222.     void PaintInto(HDC hDC);
  223.  
  224. protected:
  225.     // internal/derived-customization operations
  226.     void SetWidth(int);
  227.     int GetWidth() const {return m_nWidth;}
  228.     void SetHeight(int);
  229.     int GetHeight() const {return m_nHeight;}
  230.     void SetIndent(int);
  231.     int GetIndent() const {return m_nIndent;}
  232.     void SetChildIndent(int);
  233.     int GetChildIndent() const {return m_nChildIndent;}
  234.  
  235.     // customization
  236.     virtual void OnPaint(HDC hDC);
  237.     virtual void OnMouseOver(POINT point, WPARAM fwKeys) {}
  238.     virtual void OnClick(POINT point, WPARAM fwKeys, BOOL bLeft);
  239.  
  240.     // expansion customization
  241. public: virtual BOOL IsExpandable() {return GetFirstChild() != NULL;}
  242. protected:
  243.     virtual void OnExpand() {}
  244.     virtual void OnCollapse() {}
  245.  
  246.     // finding
  247.     virtual BOOL FoundItem(DWORD dwUser, void *pUser) const {return FALSE;}
  248.  
  249.     // event notification firing
  250.     void FireClick(POINT point, WPARAM fwKeys, BOOL bLeft);
  251.     BOOL FireOwnerDraw(HDC hDC);
  252.  
  253. private:
  254.     // caption
  255.     LPTSTR m_ptszCaption;
  256.     CAPTIONLOOK m_clNormal, m_clSelected;
  257.     RECT m_margin;
  258.  
  259.     // user data
  260.     GUID m_UserGUID;
  261.     void *m_pUserData;
  262.  
  263.     // raw characteristics
  264.     int m_nWidth;       // item's width (used only to provide horizontal scrolling as necessary)
  265.     int m_nHeight;      // item's height (not including children)
  266.     int m_nIndent;      // indent of this item relative to parent's child indent origin (full origin = this + parent origin + parent child indent)
  267.     int m_nChildIndent; // indentation of this item's children (relative to this's origin)
  268.  
  269.     // calced characteristics
  270.     int m_nBranchHeight;  // height of item and all currently expanded children
  271.  
  272.     // calced positioning
  273.     POINT m_origin;  // relative to ideal tree origin
  274.  
  275.     // state
  276.     BOOL m_bExpanded;  // is branch expanded/children shown?
  277.  
  278.     // family
  279.     CFlexTree *m_pTree;
  280.     CFTItem *m_pParent, *m_pPrev, *m_pNext, *m_pFirst, *m_pLast;
  281.  
  282.     // root
  283.     BOOL IsRoot() const;
  284.     void SetRoot(CFlexTree *);
  285.  
  286.     // helpers
  287.     void SetTree(CFlexTree *);
  288.     BOOL Attach(CFTItem *pParent, CFTItem *pPrev, CFTItem *pNext);
  289.     void SetTreeDirty(CFlexTree *pTree = NULL);
  290.     void RecalcText();
  291.     void Init();
  292.     void SelChangedInternal();
  293.     void InternalExpand(BOOL bExpand, BOOL bAll);
  294. };
  295.  
  296.  
  297. #endif //__FLEXTREE_H__
  298.